package builder.AssetFormBuilderPackage;

import handler.assetFormHandlers.AFTextFieldKeyListener;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.JTextField;

import customGUI.DatePicker;

import builder.FormBuilder;

import other.CodeDescriptionPair;

import checker.ValidLengthAndCharChecker;

import form.FormComboBox;
import form.FormDatePicker;
import form.FormTextField;
import form.AssetForm.AssetForm;

public class CoreAssetFormBuilder extends FormBuilder {

	GridBagConstraints c = new GridBagConstraints();
	
	public CoreAssetFormBuilder(){
		super(new AssetForm());
	}
	
	@Override
	public void setFormSettings() {
		form.setLayout(new GridBagLayout());
	}

	@Override
	public void buildComponents() {
		AssetForm assetForm = (AssetForm) form;
		buildNameTF(assetForm);
		buildOwnerTF(assetForm);
		buildCustodianTF(assetForm);
		buildStorageLocationTF(assetForm);
		buildTypeCB(assetForm);
		buildMaintenanceCB(assetForm);
		buildFinancialCB(assetForm);
		buildConfidentialityCB(assetForm);
		buildIntegrityCB(assetForm);
		buildAvailabilityCB(assetForm);
		buildClassificationCB(assetForm);
		buildDateAcquired(assetForm);
		buildRententionPeriod(assetForm);
		assetForm.addComponentsToList();
	}

	public void buildNameTF(AssetForm assetForm){
		JTextField txtName = new JTextField();
		txtName.setColumns(10);
		
		FormTextField nameTF = new FormTextField("Name:", txtName, new ValidLengthAndCharChecker(ValidLengthAndCharChecker.ALPHABET+" ", 30));
		assetForm.setAssetName(nameTF);
		
		txtName.addKeyListener(new AFTextFieldKeyListener(nameTF));
		
		
		c.anchor = GridBagConstraints.WEST;
		c.gridx = 0;
		c.gridy = 1;
		assetForm.add(nameTF, c);
	}
	
	public void buildOwnerTF(AssetForm assetForm){
		JTextField txtOwner = new JTextField();
		txtOwner.setColumns(10);
		
		FormTextField ownerTF = new FormTextField("Owner:", txtOwner, new ValidLengthAndCharChecker(ValidLengthAndCharChecker.ALPHABET+" ", 30));
		assetForm.setOwner(ownerTF);
		
		txtOwner.addKeyListener(new AFTextFieldKeyListener(ownerTF));
		
		c.gridx = 0;
		c.gridy = 2;
		assetForm.add(ownerTF, c);
	}
	
	public void buildCustodianTF(AssetForm assetForm){
		JTextField txtCustodian = new JTextField();
		txtCustodian.setColumns(10);
		
		FormTextField custodianTF = new FormTextField("Custodian:", txtCustodian, new ValidLengthAndCharChecker(ValidLengthAndCharChecker.ALPHABET+" ", 30));
		assetForm.setCustodian(custodianTF);
		
		txtCustodian.addKeyListener(new AFTextFieldKeyListener(custodianTF));
		
		c.gridx = 0;
		c.gridy = 3;
		assetForm.add(custodianTF, c);
	}
	
	public void buildStorageLocationTF(AssetForm assetForm){
		JTextField txtStorageLocation = new JTextField();
		txtStorageLocation.setColumns(10);
		
		FormTextField storageLocationTF = new FormTextField("Storage Location:", txtStorageLocation, new ValidLengthAndCharChecker(ValidLengthAndCharChecker.ALPHABET+" ", 30));
		assetForm.setStorageLocation(storageLocationTF);
		
		txtStorageLocation.addKeyListener(new AFTextFieldKeyListener(storageLocationTF));
		c.gridx = 0;
		c.gridy = 4;
		assetForm.add(storageLocationTF, c);
	}
	
	public void buildTypeCB(AssetForm assetForm){
		JComboBox<String> comboType = new JComboBox<String>();
		initComboBoxChoices(comboType, CodeDescriptionPair.TABLE_TYPE);
		
		FormComboBox typeCB = new FormComboBox("Type:", comboType, null);
		assetForm.setType(typeCB);
		c.gridx = 1;
		c.gridy = 1;
		assetForm.add(typeCB, c);
	}
	
	public void buildMaintenanceCB(AssetForm assetForm){
		JComboBox<String> comboMaintenance = new JComboBox<String>();
		initComboBoxChoices(comboMaintenance, CodeDescriptionPair.TABLE_MINTENANCE);

		FormComboBox maintenanceCB = new FormComboBox("Maintenance:", comboMaintenance, null);
		assetForm.setMaintenanceSched(maintenanceCB);
		c.gridx = 1;
		c.gridy = 2;
		assetForm.add(maintenanceCB, c);
	
	
	}
	
	public void buildFinancialCB(AssetForm assetForm){
		JComboBox<String> comboFinancial = new JComboBox<String>();
		initComboBoxChoices(comboFinancial, CodeDescriptionPair.TABLE_FINANCIAL);

		FormComboBox financialCB = new FormComboBox("Financial:", comboFinancial, null);
		assetForm.setFinancialVal(financialCB);
		c.gridx = 1;
		c.gridy = 3;
		assetForm.add(financialCB, c);
	}
	
	public void buildConfidentialityCB(AssetForm assetForm){
		JComboBox<String> comboConfidentiality = new JComboBox<String>();
		initComboBoxChoices(comboConfidentiality, CodeDescriptionPair.TABLE_CONFIDENTIALITY);
		
		FormComboBox confidentialityCB = new FormComboBox("Confidentiality:", comboConfidentiality, null);
		assetForm.setConfidentialityVal(confidentialityCB);
		c.gridx = 1;
		c.gridy = 4;
		assetForm.add(confidentialityCB, c);
	}
	
	public void buildIntegrityCB(AssetForm assetForm){
		JComboBox<String> comboIntegrity = new JComboBox<String>();
		initComboBoxChoices(comboIntegrity, CodeDescriptionPair.TABLE_INTEGRITY);
		
		FormComboBox integrityCB = new FormComboBox("Integrity:", comboIntegrity, null);
		assetForm.setIntegrityVal(integrityCB);
		c.gridx = 1;
		c.gridy = 5;
		assetForm.add(integrityCB, c);
	}
	
	public void buildAvailabilityCB(AssetForm assetForm){
		JComboBox<String> comboAvailability = new JComboBox<String>();
		initComboBoxChoices(comboAvailability, CodeDescriptionPair.TABLE_AVAILABILITY);
		
		FormComboBox availabilityCB = new FormComboBox("Availability:", comboAvailability, null);
		assetForm.setAvailabilityVal(availabilityCB);
		c.gridx = 1;
		c.gridy = 6;
		assetForm.add(availabilityCB, c);
	}
	
	public void buildClassificationCB(AssetForm assetForm){
		JComboBox<String> comboClassification = new JComboBox<String>();
		initComboBoxChoices(comboClassification, CodeDescriptionPair.TABLE_CLASSIFICATION);
		
		FormComboBox classificationCB = new FormComboBox("Classification:", comboClassification, null);
		assetForm.setClassification(classificationCB);
		c.gridx = 1;
		c.gridy = 7;
		assetForm.add(classificationCB, c);
	}
	
	private void buildDateAcquired(AssetForm assetForm) {
		DatePicker datePickerDateAcquired = new DatePicker();
		FormDatePicker dateAcquired  = new FormDatePicker("Date Acquired:", datePickerDateAcquired, null);
		assetForm.setDateAcquired(dateAcquired);
		c.gridx = 0;
		c.gridy = 5;
		assetForm.add(dateAcquired, c);
	}
	
	private void buildRententionPeriod(AssetForm assetForm) {
		DatePicker datePickerRetentionPeriod = new DatePicker();
		FormDatePicker retentionPeriod  = new FormDatePicker("Rentention Period:", datePickerRetentionPeriod, null);
		assetForm.setRetentionPeriod(retentionPeriod);
		c.gridx = 0;
		c.gridy = 6;
		assetForm.add(retentionPeriod, c);
	}


	//get refTableName from CodeDescriptionPair
	private void initComboBoxChoices(JComboBox<String> comboBox, String refTableName){
		ArrayList<CodeDescriptionPair> typeCDP = CodeDescriptionPair.getCodeDescriptionPairsFor(refTableName);
		
		for(int i=0;i<typeCDP.size();i++)
			comboBox.addItem(typeCDP.get(i).getDescription());
	}
		
	
}
